package zgs;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;
    public Client() {
        try {
            System.out.println("正在链接服务器....");
            socket = new Socket("172.96.1.125", 8088);
            System.out.println("与服务器链接成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("开始聊天吧");
            OutputStream out = socket.getOutputStream();
            while (true){
                System.out.print("请输入：");
                String line = scan.nextLine();
                byte[] data = line.getBytes(StandardCharsets.UTF_8);
                out.write(data.length);
                out.write(data);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

}
